package com.block.im.service.im;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.im.config.IMConfig;
import com.block.im.constant.IMURIConstant;
import com.block.im.enums.UserTypeEnum;
import com.block.im.model.dto.IMUserInfoDTO;
import com.block.im.service.IImUserSigInfoService;
import com.block.im.service.IMHandleService;
import com.tencentyun.TLSSigAPIv2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TencentIMService implements IMHandleService {

    @Autowired
    IMConfig imConfig;
    @Autowired
    IImUserSigInfoService imUserSigInfoService;

    @Override
    public void accountImportMulti(){

        IMUserInfoDTO imUserInfoDTO = imUserSigInfoService.genUserSig(imConfig.getAdminAccountId(), UserTypeEnum.ROOT, false);
        String uriParam = this.getUriParam(imUserInfoDTO.getUserSig(), imUserInfoDTO.getUserId());
        String requestUrl = StrUtil.format(IMURIConstant.BASE_DOMAIN_URL, IMURIConstant.ACCOUNT_IMPORT_MULTI, uriParam);

        List<JSONObject> userList = new ArrayList<>();

        JSONObject user1 = new JSONObject();
        user1.put("UserID", "renquan0");
        userList.add(user1);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("AccountList", userList);

        System.out.println(JSON.toJSONString(paramMap));
        String body = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(paramMap))
                .execute()
                .body();
        System.out.println(body);
    }


    /**
     * 获取请求的get参数
     * @return
     */
    public String getUriParam(String adminUserSig, String identifier){
        String content = "?sdkappid={}&identifier={}&usersig={}&random={}&contenttype=json";
        String random = String.valueOf(RandomUtil.randomInt(Integer.MAX_VALUE));
        return StrUtil.format(content, imConfig.getSdkAppId(), identifier, adminUserSig, random);
    }

}
